<?php
/**
 * FloatDotFixable Behavior class file.
 *
 * @filesource
 * @author Serge Rodovnichenko
 * @link
 * @version	$Revision$
 * @license	http://www.opensource.org/licenses/mit-license.php The MIT License
 * @package app
 * @subpackage app.models.behaviors
 */

class FloatDotFixableBehavior extends ModelBehavior
{
	var $__settings = array();
	
	function setup(&$Model, $settings=array())
	{
		$default = array();
		
		if (!isset($this->__settings[$Model->alias]))
		{
			if (empty($settings) && $Model->_schema)
			{
				foreach($Model->_schema as $fldname=>$fldopts)
				{
					if($fldopts['type'] === 'float' || $fldopts['type'] === 'double')
					{
						$settings['fields'][] = $fldname;
					}
				}
			}
			
			$this->__settings[$Model->alias] = $settings;
		}		
	}
	
	function beforeSave(&$Model)
	{
	
		$return = parent::beforeSave($Model);

        if (array_key_exists('fields', $this->__settings[$Model->alias]))
        {
            foreach($this->__settings[$Model->alias]['fields'] as $field)
            {
                if ($Model->hasField($field) && array_key_exists($field, $Model->data[$Model->alias]))
                {
                    $Model->data[$Model->alias][$field] = str_replace(',', '.', $Model->data[$Model->alias][$field]);
                }
            }
        }

		return $return;
	}
}

?>